//
//  JKViewMineHeader.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/23.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKViewMineHeader.h"

#import "ByUIMineDataVC.h"

@implementation JKViewMineHeader
{
    UIImageView *_iconView;     // 头像
    UILabel     *_nickLabel;    // 昵称
    UILabel     *_descLabel;    // 介绍
    UILabel     *_moneyLabel;   // 我的红包
}


- (instancetype)init {
    
    if (self = [super init]) {
        
        [self addTarget:self action:@selector(selfTap) forControlEvents:UIControlEventTouchUpInside];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _iconView = [[UIImageView alloc] init];
    _iconView.image = DEFAULT_IMAGE;
    [self addSubview:_iconView];
    
    _nickLabel = [[UILabel alloc] init];
    _nickLabel.font = FONT(15);
    _nickLabel.text = @"昵称";
    _nickLabel.textAlignment = NSTextAlignmentCenter;
    _nickLabel.textColor = [UIColor blackColor];
    [self addSubview:_nickLabel];

    _descLabel = [[UILabel alloc] init];
    _descLabel.font = FONT(15);
    _descLabel.text = @"介绍";
    _descLabel.numberOfLines = 0;
    _descLabel.textAlignment = NSTextAlignmentCenter;
    _descLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:_descLabel];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.font = FONT(15);
    _moneyLabel.text = @"我的红包：￥8000";
    _moneyLabel.textAlignment = NSTextAlignmentCenter;
    _moneyLabel.textColor = rgba(226, 96, 91, 1);
    [self addSubview:_moneyLabel];
    
    // 线条
    UIView *line = [UIView new];
    line.backgroundColor = BY_LINE_COLOR;
    [self addSubview:line];
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@70);
        make.centerX.equalTo(@0);
        make.top.equalTo(@20);
        
        _iconView.layer.cornerRadius = 35;
        _iconView.layer.masksToBounds = true;
    }];
    [_nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.top.equalTo(_iconView.mas_bottom).offset(10);
    }];
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_nickLabel);
        make.top.equalTo(_nickLabel.mas_bottom).offset(5);
    }];
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_descLabel);
        make.top.equalTo(_descLabel.mas_bottom).offset(5);
    }];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.top.equalTo(_moneyLabel.mas_bottom).offset(10);
        make.height.equalTo(@1);
        
        make.bottom.equalTo(@0);
    }];
    
    
}

- (void)setModel:(ByModelUser *)model {
    
    _model = model;
    
    if (model) {
        _nickLabel.text = model.nickname;
        [_iconView setImageWithURL:[NSURL URLWithString:model.head] placeholderImage:DEFAULT_IMAGE completed:nil];
        NSString *pointStr;
        if (model.points) {
            pointStr = [NSString stringWithFormat:@"%.2f", [model.points floatValue] / 100.0];
        }else{
            pointStr = @"0";
        }
        NSString *str = [NSString stringWithFormat:@"我的红包：%@￥",pointStr];

        _moneyLabel.text = str;
        _descLabel.text = model.sign;
    } else {
        _nickLabel.text = @"登录";
        _iconView.image = DEFAULT_IMAGE;
        _moneyLabel.text = @"";
        _descLabel.text = @"介绍";
    }
    
}
- (void)selfTap {
    if (!BY_USER) {
        ByUILoginVC *vc = [[ByUILoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self.viewController presentViewController:navi animated:YES completion:nil];
        return;
    }
    ByUIMineDataVC *vc = [[ByUIMineDataVC alloc] init];
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
}

@end
